function broadcast (componentname, event, params) {
  this.$children.forEach(child => {
    var name = child.$options.componentname

    if (name === componentname) {
      child.$emit.apply(child, [event].concat(params))
      broadcast.apply(child, [componentname, event].concat([params]))
    } else {
      broadcast.apply(child, [componentname, event].concat([params]))
    }
  })
}
export default {
  methods: {
    dispatch (componentname, event, params) {
      var parent = this.$parent || this.$root
      var name = parent.$options.componentname

      while (parent && (!name || name !== componentname)) {
        parent = parent.$parent

        if (parent) {
          name = parent.$options.componentname
        }
      }
      if (parent) {
        parent.$emit.apply(parent, [event].concat(params))
      }
    },
    broadcast (componentname, event, params) {
      broadcast.call(this, componentname, event, params)
    }
  }
}
